package apps.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import webframe.common.helper.ControllerHelper;
import webframe.common.helper.FormatTransformHelper;
import apps.web.controller.base.MyBaseCommandController;
import apps.web.model.GoodsClearItem;
import apps.web.model.PassageBill;

public class EntGoodsClearController extends MyBaseCommandController {

	@Override
	protected ModelAndView handleInner(HttpServletRequest request, HttpServletResponse response,
			Object command) throws RuntimeException {
		GoodsClearItem item = (GoodsClearItem) command;
		Long declId = ControllerHelper.extractId(request, "declId");
		Long goodsId = ControllerHelper.extractId(request, "goodsId");

		List<PassageBill> passagelist = getPassageBills(item);

		if (item.getClearFlag().intValue() == 2 || item.getClearFlag().intValue() == 4) {
			// ClearFlag 暂存2 提交4
			getServiceContainer().getCheckBizService().updateEntGoodsClear(declId, goodsId, item,
					passagelist);
		}
		return new ModelAndView(new RedirectView("EntGoodsClearList.html?id=" + declId));
	}

	// 将passageNo转化为list
	public static List<PassageBill> getPassageBills(GoodsClearItem item) {
		List<PassageBill> list = new ArrayList<PassageBill>();
		String passageNo = item.getPassageNo();
		String[] sa = passageNo.split(FormatTransformHelper.SEG);
		for (String s : sa) {
			if (s != null && s.trim().length() != 0) {
				PassageBill p = new PassageBill();
				p.setDeclNo(item.getDeclNo());
				p.setGoodsNo(item.getGoodsNo());
				p.setPassageNo(s);
				list.add(p);
			}
		}
		return list;
	}
}